package cn.com.cfae.iras.doc.analyze.mq.producer;

import cn.com.cfae.iras.doc.analyze.service.integrityregulation.IntegrityRegulationService;
import com.alibaba.fastjson.JSONObject;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by 佟媛媛 on 18/10/23.
 * 文本分析生产者
 */

@RestController
@RequestMapping("/api/v1")
public class DocAnalyzeProducer {

    private static Logger log = LoggerFactory.getLogger(DocAnalyzeProducer.class);

    @Autowired
    @Qualifier( "analyzeStatusProducer")
    private DefaultMQProducer analyzeStatusProducer;

    @Value("${rocketmq.topic.analyze_status}")
    private String topicAnalyzeStatus;

    @Autowired
    private IntegrityRegulationService integrityRegulationService;

    @RequestMapping("/test")
    public Object test() throws Exception{
//        List<Map> list = integrityRegulationService.getComponentList("M");
//        DocumentModel documentModel = new DocumentModel();
//        documentModel.setDocumentName("tyy");
//        Class I1001 = Class.forName("cn.com.cfae.iras.doc.analyze.components.I1001");
//        Object obj =  ApplicationContextHelper.getBean("I1001");
//        Method m = I1001.getMethod("setExtractData",DocumentModel.class);
//        m.invoke(obj,documentModel);
//        Method m1 = I1001.getMethod("handlerM");
//        m1.invoke(obj);

//        第一种方式
//        Class component = Class.forName("cn.com.cfae.iras.doc.analyze.components.I1001");
//        Object instance = component.newInstance();
//        Method[] methods = component.getDeclaredMethods();
//        Method setExtractData = component.getMethod("setExtractData",DocumentModel.class);
//        setExtractData.invoke(instance,documentModel);
//        Method method = null;
//        for(Method m : methods){
//            if(("handler"+"M").equals(m.getName())){
//                method = m; break;
//            }
//        }
//        if(method!=null){
//            method.invoke(instance);
//        }
//        return integrityRegulationService.test();
        JSONObject returnMsg = new JSONObject();
        returnMsg.put("code",0);
        sendMsg(returnMsg,"*");
        return null;
    }


    public void sendMsg(JSONObject message,String tag) throws Exception{
        Message msg = new Message(topicAnalyzeStatus, tag, message.toJSONString().getBytes("UTF-8"));
        analyzeStatusProducer.send(msg,new SendCallback(){

            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("生产者向"+topicAnalyzeStatus+"["+null+"]"+"发送消息："+message+";发送状态："+sendResult.getSendStatus());
            }

            @Override
            public void onException(Throwable e) {
                log.error(e.getMessage(),e);
            }
        });
    }
}
